package com.aat.dxfy.game.blankwhite;

public class MaxminAction {
	// 一个子附近有8个方位
	private int[][] up = new int[8][8];
	private int[][] down = new int[8][8];
	private int[][] left = new int[8][8];
	private int[][] right = new int[8][8];
	private int[][] upleft = new int[8][8];
	private int[][] upright = new int[8][8];
	private int[][] downleft = new int[8][8];
	private int[][] downright = new int[8][8];
	private int chess;
	public void setChess(int chess) {
		this.chess = chess;
	}
	private int INF = 10000000;// 概率数乘以的参数
	private int BRANCHES = 1000000;// 记录当前节点还可以向下搜索的总节点数,采用的做法为Min-Max搜索算法
	// 权值表
	private int[][] cellpoints = { { 100, -5, 10, 5, 5, 10, -5, 100 }, { -5, -45, 1, 1, 1, 1, -45, -5 },
			{ 10, 1, 3, 2, 2, 3, 1, 10 }, { 5, 1, 2, 1, 1, 2, 1, 5 }, { 5, 1, 2, 1, 1, 2, 1, 5 },
			{ 10, 1, 3, 2, 2, 3, 1, 10 }, { -5, -45, 1, 1, 1, 1, -45, -5 }, { 100, -5, 10, 5, 5, 10, -5, 100 } };

	/**
	 * 根据可下子位置，判断权值
	 * 
	 * @param color
	 * @param branches
	 * @param chessboard
	 * @param stop
	 * @param a
	 * @param b
	 * @param fa
	 * @return
	 */
	public int dfs(int color, int branches, int[][] chessboard, boolean stop, int a, int b, int fa) {
		if (branches == 0) {
			int a1 = 0;
			for (int i = 0; i < 8; i++) {
				for (int j = 0; j < 8; j++) {
					if (chessboard[i][j] == this.chess)
						a1 += this.cellpoints[i][j];
					else if (chessboard[i][j] == 1 - this.chess) {
						a1 -= this.cellpoints[i][j];
					}
				}
			}
			int a2 = judgeStatic(chessboard);
			int p1 = 3;
			int p2 = 7;
			int ans = p1 * a1 + p2 * a2;
			return ans;
		}
		int min = this.INF;
		int max = -this.INF;
		int rear = 0;
		int[] q_x = new int[64];
		int[] q_y = new int[64];
		int[] q_up = new int[64];
		int[] q_down = new int[64];
		int[] q_left = new int[64];
		int[] q_right = new int[64];
		int[] q_upleft = new int[64];
		int[] q_upright = new int[64];
		int[] q_downleft = new int[64];
		int[] q_downright = new int[64];

		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				if (chessboard[i][j] == -1) {
					boolean ok = false;
					if (i != 0) {
						int k;
						for (k = i - 1; (k > 0) && (chessboard[k][j] == 1 - color); k--)
							;
						if ((k != i - 1) && (chessboard[k][j] == color)) {
							q_up[rear] = 1;
							ok = true;
						} else {
							q_up[rear] = 0;
						}
					}
					if (i != 7) {
						int k;
						for (k = i + 1; (k < 7) && (chessboard[k][j] == 1 - color); k++)
							;
						if ((k != i + 1) && (chessboard[k][j] == color)) {
							q_down[rear] = 1;
							ok = true;
						} else {
							q_down[rear] = 0;
						}
					}
					if (j != 0) {
						int k;
						for (k = j - 1; (k > 0) && (chessboard[i][k] == 1 - color); k--)
							;
						if ((k != j - 1) && (chessboard[i][k] == color)) {
							q_left[rear] = 1;
							ok = true;
						} else {
							q_left[rear] = 0;
						}
					}
					if (j != 7) {
						int k;
						for (k = j + 1; (k < 7) && (chessboard[i][k] == 1 - color); k++)
							;
						if ((k != j + 1) && (chessboard[i][k] == color)) {
							q_right[rear] = 1;
							ok = true;
						} else {
							q_right[rear] = 0;
						}
					}
					if ((i != 0) && (j != 0)) {
						int k = i - 1;
						int m;
						for (m = j - 1; (k > 0) && (m > 0) && (chessboard[k][m] == 1 - color); m--)
							k--;
						if ((k != i - 1) && (chessboard[k][m] == color)) {
							q_upleft[rear] = 1;
							ok = true;
						} else {
							q_upleft[rear] = 0;
						}
					}
					if ((i != 0) && (j != 7)) {
						int k = i - 1;
						int m;
						for (m = j + 1; (k > 0) && (m < 7) && (chessboard[k][m] == 1 - color); m++)
							k--;
						if ((k != i - 1) && (chessboard[k][m] == color)) {
							q_upright[rear] = 1;
							ok = true;
						} else {
							q_upright[rear] = 0;
						}
					}
					if ((i != 7) && (j != 0)) {
						int k = i + 1;
						int m;
						for (m = j - 1; (k < 7) && (m > 0) && (chessboard[k][m] == 1 - color); m--)
							k++;
						if ((k != i + 1) && (chessboard[k][m] == color)) {
							q_downleft[rear] = 1;
							ok = true;
						} else {
							q_downleft[rear] = 0;
						}
					}
					if ((i != 7) && (j != 7)) {
						int k = i + 1;
						int m;
						for (m = j + 1; (k < 7) && (m < 7) && (chessboard[k][m] == 1 - color); m++)
							k++;
						if ((k != i + 1) && (chessboard[k][m] == color)) {
							q_downright[rear] = 1;
							ok = true;
						} else {
							q_downright[rear] = 0;
						}
					}
					if (ok) {
						q_x[rear] = i;
						q_y[rear] = j;
						rear++;
					}

				}

			}

		}

		for (int q = 0; q < rear; q++) {
			int[][] board = new int[8][8];
			for (int i = 0; i < 8; i++) {
				for (int j = 0; j < 8; j++)
					board[i][j] = chessboard[i][j];
			}
			int i = q_x[q];
			int j = q_y[q];

			board[i][j] = color;

			if (q_up[q] == 1) {
				int r = i - 1;
				for (int c = j; board[r][c] != color; r--)
					board[r][c] = color;
			}
			if (q_down[q] == 1) {
				int r = i + 1;
				for (int c = j; board[r][c] != color; r++)
					board[r][c] = color;
			}
			if (q_left[q] == 1) {
				int r = i;
				for (int c = j - 1; board[r][c] != color; c--)
					board[r][c] = color;
			}
			if (q_right[q] == 1) {
				int r = i;
				for (int c = j + 1; board[r][c] != color; c++)
					board[r][c] = color;
			}
			if (q_upleft[q] == 1) {
				int r = i - 1;
				for (int c = j - 1; board[r][c] != color; c--) {
					board[r][c] = color;

					r--;
				}
			}
			if (q_upright[q] == 1) {
				int r = i - 1;
				for (int c = j + 1; board[r][c] != color; c++) {
					board[r][c] = color;

					r--;
				}
			}
			if (q_downleft[q] == 1) {
				int r = i + 1;
				for (int c = j - 1; board[r][c] != color; c--) {
					board[r][c] = color;

					r++;
				}
			}
			if (q_downright[q] == 1) {
				int r = i + 1;
				for (int c = j + 1; board[r][c] != color; c++) {
					board[r][c] = color;

					r++;
				}
			}

			int temp = dfs(1 - color, branches / rear, board, false, a, b, color);

			if (fa == this.chess) {
				if (color == 1 - this.chess) {
					if (temp < b) {
						if (temp <= a)
							return temp;
						b = temp;
					}
					if (temp < min)
						min = temp;
				} else if (color == this.chess) {
					if (temp > a) {
						if (temp > b)
							b = temp;
						a = temp;
					}
					if (temp > max)
						max = temp;
				}
			} else if (fa == 1 - this.chess) {
				if (color == this.chess) {
					if (temp > a) {
						if (temp >= b)
							return temp;
						a = temp;
					}
					if (temp > max)
						max = temp;
				} else if (color == 1 - this.chess) {
					if (temp < b) {
						if (temp < a)
							a = temp;
						b = temp;
					}
					if (temp < min) {
						min = temp;
					}
				}
			}
		}
		if (rear == 0) {
			if (!stop) {
				return dfs(fa, branches, chessboard, true, a, b, fa);
			}

			int bot_chess = 0;
			int player_chess = 0;
			for (int i = 0; i < 8; i++)
				for (int j = 0; j < 8; j++) {
					if (chessboard[i][j] == this.chess)
						bot_chess++;
					else if (chessboard[i][j] == 1 - this.chess)
						player_chess++;
				}
			if (bot_chess > player_chess)
				return this.INF / 10;
			if (player_chess > bot_chess) {
				return -this.INF / 10;
			}
			return 0;
		}
		int ans;
		if (color == 1 - this.chess)
			ans = min;
		else
			ans = max;
		return ans;
	}

	/**
	 * 黑白棋AI算法
	 */
	public String bot_judge(int[][] box) {
		String res = "NO";
		// 算法
		int max_value = -this.INF;
		int a = -this.INF;
		int b = this.INF;
		for (int i = 0; i < 8; i++){
			for (int j = 0; j < 8; j++){
				if (judgeLaychess(i, j, box, this.chess)) {
					// 判断可下子的位置
					int[][] board = new int[8][8];
					for (int p = 0; p < 8; p++)// 循环出整个棋盘
						for (int q = 0; q < 8; q++)
							board[p][q] = box[p][q];
					int color = this.chess;
					board[i][j] = color;
					if (this.up[i][j] == 1) {
						int r = i - 1;
						for (int c = j; board[r][c] != color; r--)
							board[r][c] = color;
					}
					if (this.down[i][j] == 1) {
						int r = i + 1;
						for (int c = j; board[r][c] != color; r++)
							board[r][c] = color;
					}
					if (this.left[i][j] == 1) {
						int r = i;
						for (int c = j - 1; board[r][c] != color; c--)
							board[r][c] = color;
					}
					if (this.right[i][j] == 1) {
						int r = i;
						for (int c = j + 1; board[r][c] != color; c++)
							board[r][c] = color;
					}
					if (this.upleft[i][j] == 1) {
						int r = i - 1;
						for (int c = j - 1; board[r][c] != color; c--) {
							board[r][c] = color;
							r--;
						}
					}
					if (this.upright[i][j] == 1) {
						int r = i - 1;
						for (int c = j + 1; board[r][c] != color; c++) {
							board[r][c] = color;
							r--;
						}
					}
					if (this.downleft[i][j] == 1) {
						int r = i + 1;
						for (int c = j - 1; board[r][c] != color; c--) {
							board[r][c] = color;
							r++;
						}
					}
					if (this.downright[i][j] == 1) {
						int r = i + 1;
						for (int c = j + 1; board[r][c] != color; c++) {
							board[r][c] = color;
							r++;
						}
					}
					// 根据权值表，进行
					int temp = dfs(1 - color, this.BRANCHES, board, false, a, b, color);
					if (temp > a)
						a = temp;
					if (temp > max_value) {// 确定下子的位置
						max_value = temp;
						// i,j
						res = Integer.toString(i) + (char) (j + 'A' - 1);
					}
				}
			}
		}
		return res;
	}

	/**
	 * 判断棋是否能走
	 * 
	 * @param i
	 * @param j
	 * @param chessboard
	 * @param color
	 * @return
	 */
	public boolean judgeLaychess(int i, int j, int[][] chessboard, int color) {
		boolean ok = false;
		if (chessboard[i][j] != -1) {
			return ok;
		}
		if (i != 0) {
			int k;
			for (k = i - 1; (k > 0) && (chessboard[k][j] == 1 - color); k--)
				;
			if ((k != i - 1) && (chessboard[k][j] == color)) {
				this.up[i][j] = 1;
				ok = true;
			} else {
				this.up[i][j] = 0;
			}
		}
		if (i != 7) {
			int k;
			for (k = i + 1; (k < 7) && (chessboard[k][j] == 1 - color); k++)
				;
			if ((k != i + 1) && (chessboard[k][j] == color)) {
				this.down[i][j] = 1;
				ok = true;
			} else {
				this.down[i][j] = 0;
			}
		}
		if (j != 0) {
			int k;
			for (k = j - 1; (k > 0) && (chessboard[i][k] == 1 - color); k--)
				;
			if ((k != j - 1) && (chessboard[i][k] == color)) {
				this.left[i][j] = 1;
				ok = true;
			} else {
				this.left[i][j] = 0;
			}
		}
		if (j != 7) {
			int k;
			for (k = j + 1; (k < 7) && (chessboard[i][k] == 1 - color); k++)
				;
			if ((k != j + 1) && (chessboard[i][k] == color)) {
				this.right[i][j] = 1;
				ok = true;
			} else {
				this.right[i][j] = 0;
			}
		}
		if ((i != 0) && (j != 0)) {
			int k = i - 1;
			int m;
			for (m = j - 1; (k > 0) && (m > 0) && (chessboard[k][m] == 1 - color); m--)
				k--;
			if ((k != i - 1) && (chessboard[k][m] == color)) {
				this.upleft[i][j] = 1;
				ok = true;
			} else {
				this.upleft[i][j] = 0;
			}
		}
		if ((i != 0) && (j != 7)) {
			int k = i - 1;
			int m;
			for (m = j + 1; (k > 0) && (m < 7) && (chessboard[k][m] == 1 - color); m++)
				k--;
			if ((k != i - 1) && (chessboard[k][m] == color)) {
				this.upright[i][j] = 1;
				ok = true;
			} else {
				this.upright[i][j] = 0;
			}
		}
		if ((i != 7) && (j != 0)) {
			int k = i + 1;
			int m;
			for (m = j - 1; (k < 7) && (m > 0) && (chessboard[k][m] == 1 - color); m--)
				k++;
			if ((k != i + 1) && (chessboard[k][m] == color)) {
				this.downleft[i][j] = 1;
				ok = true;
			} else {
				this.downleft[i][j] = 0;
			}
		}
		if ((i != 7) && (j != 7)) {
			int k = i + 1;
			int m;
			for (m = j + 1; (k < 7) && (m < 7) && (chessboard[k][m] == 1 - color); m++)
				k++;
			if ((k != i + 1) && (chessboard[k][m] == color)) {
				this.downright[i][j] = 1;
				ok = true;
			} else {
				this.downright[i][j] = 0;
			}
		}
		return ok;
	}

	public int judgeStatic(int[][] chessboard) {
		int ans = 0;

		for (int i = 0; i < 8; i++)
			for (int j = 0; j < 8; j++) {
				boolean flag = false;
				if (chessboard[i][j] == this.chess) {
					if ((i > 0) && (chessboard[(i - 1)][j] == -1))
						flag = true;
					else if ((i < 7) && (chessboard[(i + 1)][j] == -1))
						flag = true;
					else if ((j > 0) && (chessboard[i][(j - 1)] == -1))
						flag = true;
					else if ((j < 7) && (chessboard[i][(j + 1)] == -1))
						flag = true;
					if (flag)
						ans--;
				} else if (chessboard[i][j] == 1 - this.chess) {
					if ((i > 0) && (chessboard[(i - 1)][j] == -1))
						flag = true;
					else if ((i < 7) && (chessboard[(i + 1)][j] == -1))
						flag = true;
					else if ((j > 0) && (chessboard[i][(j - 1)] == -1))
						flag = true;
					else if ((j < 7) && (chessboard[i][(j + 1)] == -1))
						flag = true;
					if (flag)
						ans++;
				}
			}
		return ans;
	}

}
